home *** CD-ROM | disk | FTP | other *** search
/ Die Speccy' 97 / Die Speccy' 97.iso / amiga_system / the_aminet / dev / lang / python_src.lha / amigapython / python / ceval.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-10-22  |  60.3 KB  |  2,951 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI not be used in advertising or publicity pertaining to
  13. distribution of the software without specific, written prior permission.
  14.  
  15. STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  16. THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  18. FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  20. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  21. OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  
  23. ******************************************************************/
  24.  
  25. /* Execute compiled code */
  26.  
  27. /* XXX TO DO:
  28.    XXX how to pass arguments to call_trace?
  29.    XXX access stuff can probably dereference NULL locals?
  30.    XXX need to extend apply() to be able to pass keyword args
  31.    XXX need to be able to call built-in functions with keyword args
  32.    XXX speed up searching for keywords by using a dictionary
  33.    XXX unknown keyword shouldn't raise KeyError?
  34.    XXX document it!
  35.    */
  36.  
  37. #include "allobjects.h"
  38.  
  39. #include "import.h"
  40. #include "sysmodule.h"
  41. #include "bltinmodule.h"
  42. #include "compile.h"
  43. #include "frameobject.h"
  44. #include "eval.h"
  45. #include "ceval.h"
  46. #include "opcode.h"
  47. #include "traceback.h"
  48. #include "graminit.h"
  49. #include "pythonrun.h"
  50.  
  51. #include <ctype.h>
  52.  
  53. extern int suppress_print; /* Declared in pythonrun.c, set in pythonmain.c */
  54.  
  55. /* Turn this on if your compiler chokes on the big switch: */
  56. /* #define CASE_TOO_BIG 1 */
  57.  
  58. /* Turn this on if you want to debug the interpreter: */
  59. /* (This can be on even if NDEBUG is defined) */
  60. /* #define DEBUG 1 */
  61.  
  62. #if defined(DEBUG) || !defined(NDEBUG)
  63. /* For debugging the interpreter: */
  64. #define LLTRACE  1    /* Low-level trace feature */
  65. #define CHECKEXC 1    /* Double-check exception checking */
  66. #endif
  67.  
  68.  
  69. /* Forward declarations */
  70.  
  71. static object *eval_code2 PROTO((codeobject *,
  72.                  object *, object *,
  73.                  object **, int,
  74.                  object **, int,
  75.                  object **, int,
  76.                  object *));
  77. #ifdef LLTRACE
  78. static int prtrace PROTO((object *, char *));
  79. #endif
  80. static void call_exc_trace PROTO((object **, object**, frameobject *));
  81. static int call_trace
  82.     PROTO((object **, object **, frameobject *, char *, object *));
  83. static object *add PROTO((object *, object *));
  84. static object *sub PROTO((object *, object *));
  85. static object *mul PROTO((object *, object *));
  86. static object *divide PROTO((object *, object *));
  87. static object *mod PROTO((object *, object *));
  88. static object *neg PROTO((object *));
  89. static object *pos PROTO((object *));
  90. static object *not PROTO((object *));
  91. static object *invert PROTO((object *));
  92. static object *lshift PROTO((object *, object *));
  93. static object *rshift PROTO((object *, object *));
  94. static object *and PROTO((object *, object *));
  95. static object *xor PROTO((object *, object *));
  96. static object *or PROTO((object *, object *));
  97. static object *call_builtin PROTO((object *, object *, object *));
  98. static object *call_function PROTO((object *, object *, object *));
  99. static object *apply_subscript PROTO((object *, object *));
  100. static object *loop_subscript PROTO((object *, object *));
  101. static int slice_index PROTO((object *, int, int *));
  102. static object *apply_slice PROTO((object *, object *, object *));
  103. static int assign_subscript PROTO((object *, object *, object *));
  104. static int assign_slice PROTO((object *, object *, object *, object *));
  105. static int cmp_exception PROTO((object *, object *));
  106. static int cmp_member PROTO((object *, object *));
  107. static object *cmp_outcome PROTO((int, object *, object *));
  108. static int import_from PROTO((object *, object *, object *));
  109. static object *build_class PROTO((object *, object *, object *));
  110. static int access_statement PROTO((object *, object *, frameobject *));
  111. static int exec_statement PROTO((object *, object *, object *));
  112. static object *find_from_args PROTO((frameobject *, int));
  113.  
  114.  
  115. /* Pointer to current frame, used to link new frames to */
  116.  
  117. static frameobject *current_frame;
  118.  
  119. #ifdef WITH_THREAD
  120.  
  121. #include <errno.h>
  122. #include "thread.h"
  123.  
  124. static type_lock interpreter_lock = 0;
  125. static long main_thread = 0;
  126.  
  127. void
  128. init_save_thread()
  129. {
  130.     if (interpreter_lock)
  131.         return;
  132.     interpreter_lock = allocate_lock();
  133.     acquire_lock(interpreter_lock, 1);
  134.     main_thread = get_thread_ident();
  135. }
  136.  
  137. #endif
  138.  
  139. /* Functions save_thread and restore_thread are always defined so
  140.    dynamically loaded modules needn't be compiled separately for use
  141.    with and without threads: */
  142.  
  143. object *
  144. save_thread()
  145. {
  146. #ifdef WITH_THREAD
  147.     if (interpreter_lock) {
  148.         object *res;
  149.         res = (object *)current_frame;
  150.         current_frame = NULL;
  151.         release_lock(interpreter_lock);
  152.         return res;
  153.     }
  154. #endif
  155.     return NULL;
  156. }
  157.  
  158. void
  159. restore_thread(x)
  160.     object *x;
  161. {
  162. #ifdef WITH_THREAD
  163.     if (interpreter_lock) {
  164.         int err;
  165.         err = errno;
  166.         acquire_lock(interpreter_lock, 1);
  167.         errno = err;
  168.         current_frame = (frameobject *)x;
  169.     }
  170. #endif
  171. }
  172.  
  173.  
  174. /* Mechanism whereby asynchronously executing callbacks (e.g. UNIX
  175.    signal handlers or Mac I/O completion routines) can schedule calls
  176.    to a function to be called synchronously.
  177.    The synchronous function is called with one void* argument.
  178.    It should return 0 for success or -1 for failure -- failure should
  179.    be accompanied by an exception.
  180.  
  181.    If registry succeeds, the registry function returns 0; if it fails
  182.    (e.g. due to too many pending calls) it returns -1 (without setting
  183.    an exception condition).
  184.  
  185.    Note that because registry may occur from within signal handlers,
  186.    or other asynchronous events, calling malloc() is unsafe!
  187.  
  188. #ifdef WITH_THREAD
  189.    Any thread can schedule pending calls, but only the main thread
  190.    will execute them.
  191. #endif
  192.  
  193.    XXX WARNING!  ASYNCHRONOUSLY EXECUTING CODE!
  194.    There are two possible race conditions:
  195.    (1) nested asynchronous registry calls;
  196.    (2) registry calls made while pending calls are being processed.
  197.    While (1) is very unlikely, (2) is a real possibility.
  198.    The current code is safe against (2), but not against (1).
  199.    The safety against (2) is derived from the fact that only one
  200.    thread (the main thread) ever takes things out of the queue.
  201. */
  202.  
  203. #define NPENDINGCALLS 32
  204. static struct {
  205.     int (*func) PROTO((ANY *));
  206.     ANY *arg;
  207. } pendingcalls[NPENDINGCALLS];
  208. static volatile int pendingfirst = 0;
  209. static volatile int pendinglast = 0;
  210.  
  211. int
  212. Py_AddPendingCall(func, arg)
  213.     int (*func) PROTO((ANY *));
  214.     ANY *arg;
  215. {
  216.     static int busy = 0;
  217.     int i, j;
  218.     /* XXX Begin critical section */
  219.     /* XXX If you want this to be safe against nested
  220.        XXX asynchronous calls, you'll have to work harder! */
  221.     if (busy)
  222.         return -1;
  223.     busy = 1;
  224.     i = pendinglast;
  225.     j = (i + 1) % NPENDINGCALLS;
  226.     if (j == pendingfirst)
  227.         return -1; /* Queue full */
  228.     pendingcalls[i].func = func;
  229.     pendingcalls[i].arg = arg;
  230.     pendinglast = j;
  231.     busy = 0;
  232.     /* XXX End critical section */
  233.     return 0;
  234. }
  235.  
  236. int
  237. Py_MakePendingCalls()
  238. {
  239.     static int busy = 0;
  240. #ifdef WITH_THREAD
  241.     if (get_thread_ident() != main_thread)
  242.         return 0;
  243. #endif
  244.     if (busy)
  245.         return 0;
  246.     busy = 1;
  247.     for (;;) {
  248.         int i;
  249.         int (*func) PROTO((ANY *));
  250.         ANY *arg;
  251.         i = pendingfirst;
  252.         if (i == pendinglast)
  253.             break; /* Queue empty */
  254.         func = pendingcalls[i].func;
  255.         arg = pendingcalls[i].arg;
  256.         pendingfirst = (i + 1) % NPENDINGCALLS;
  257.         if (func(arg) < 0) {
  258.             busy = 0;
  259.             return -1;
  260.         }
  261.     }
  262.     busy = 0;
  263.     return 0;
  264. }
  265.  
  266.  
  267. /* Status code for main loop (reason for stack unwind) */
  268.  
  269. enum why_code {
  270.         WHY_NOT,    /* No error */
  271.         WHY_EXCEPTION,    /* Exception occurred */
  272.         WHY_RERAISE,    /* Exception re-raised by 'finally' */
  273.         WHY_RETURN,    /* 'return' statement */
  274.         WHY_BREAK    /* 'break' statement */
  275. };
  276.  
  277.  
  278. /* Backward compatible interface */
  279.  
  280. object *
  281. eval_code(co, globals, locals)
  282.     codeobject *co;
  283.     object *globals;
  284.     object *locals;
  285. {
  286.     return eval_code2(co,
  287.               globals, locals,
  288.               (object **)NULL, 0,
  289.               (object **)NULL, 0,
  290.               (object **)NULL, 0,
  291.               (object *)NULL);
  292. }
  293.  
  294.  
  295. /* Interpreter main loop */
  296.  
  297. static object *
  298. eval_code2(co, globals, locals,
  299.        args, argcount, kws, kwcount, defs, defcount, owner)
  300.     codeobject *co;
  301.     object *globals;
  302.     object *locals;
  303.     object **args;
  304.     int argcount;
  305.     object **kws; /* length: 2*kwcount */
  306.     int kwcount;
  307.     object **defs;
  308.     int defcount;
  309.     object *owner;
  310. {
  311.     register unsigned char *next_instr;
  312.     register int opcode;    /* Current opcode */
  313.     register int oparg;    /* Current opcode argument, if any */
  314.     register object **stack_pointer;
  315.     register enum why_code why; /* Reason for block stack unwind */
  316.     register int err;    /* Error status -- nonzero if error */
  317.     register object *x;    /* Result object -- NULL if error */
  318.     register object *v;    /* Temporary objects popped off stack */
  319.     register object *w;
  320.     register object *u;
  321.     register object *t;
  322.     register frameobject *f; /* Current frame */
  323.     register object **fastlocals;
  324.     object *retval;        /* Return value */
  325.     int defmode = 0;    /* Default access mode for new variables */
  326. #ifdef LLTRACE
  327.     int lltrace;
  328. #endif
  329. #if defined(DEBUG) || defined(LLTRACE)
  330.     /* Make it easier to find out where we are with a debugger */
  331.     char *filename = getstringvalue(co->co_filename);
  332. #endif
  333.  
  334. /* Code access macros */
  335.  
  336. #define GETCONST(i)    Getconst(f, i)
  337. #define GETNAME(i)    Getname(f, i)
  338. #define GETNAMEV(i)    Getnamev(f, i)
  339. #define FIRST_INSTR()    (GETUSTRINGVALUE(f->f_code->co_code))
  340. #define INSTR_OFFSET()    (next_instr - FIRST_INSTR())
  341. #define NEXTOP()    (*next_instr++)
  342. #define NEXTARG()    (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2])
  343. #define JUMPTO(x)    (next_instr = FIRST_INSTR() + (x))
  344. #define JUMPBY(x)    (next_instr += (x))
  345.  
  346. /* Stack manipulation macros */
  347.  
  348. #define STACK_LEVEL()    (stack_pointer - f->f_valuestack)
  349. #define EMPTY()        (STACK_LEVEL() == 0)
  350. #define TOP()        (stack_pointer[-1])
  351. #define BASIC_PUSH(v)    (*stack_pointer++ = (v))
  352. #define BASIC_POP()    (*--stack_pointer)
  353.  
  354. #define CHECK_STACK(n)    (STACK_LEVEL() + (n) < f->f_nvalues || \
  355.               (stack_pointer = extend_stack(f, STACK_LEVEL(), n)))
  356.  
  357. #ifdef LLTRACE
  358. #define PUSH(v)        (BASIC_PUSH(v), lltrace && prtrace(TOP(), "push"))
  359. #define POP()        (lltrace && prtrace(TOP(), "pop"), BASIC_POP())
  360. #else
  361. #define PUSH(v)        BASIC_PUSH(v)
  362. #define POP()        BASIC_POP()
  363. #endif
  364.  
  365. /* Local variable macros */
  366.  
  367. #define GETLOCAL(i)    (fastlocals[i])
  368. #define SETLOCAL(i, value)    do { XDECREF(GETLOCAL(i)); \
  369.                      GETLOCAL(i) = value; } while (0)
  370.  
  371.     if (globals == NULL) {
  372.         err_setstr(SystemError, "eval_code2: NULL globals");
  373.         return NULL;
  374.     }
  375.  
  376. #ifdef LLTRACE
  377.     lltrace = dictlookup(globals, "__lltrace__") != NULL;
  378. #endif
  379.  
  380.     f = newframeobject(
  381.             current_frame,        /*back*/
  382.             co,            /*code*/
  383.             globals,        /*globals*/
  384.             locals,            /*locals*/
  385.             owner,            /*owner*/
  386.             50,            /*nvalues*/
  387.             20);            /*nblocks*/
  388.     if (f == NULL)
  389.         return NULL;
  390.  
  391.     current_frame = f;
  392.  
  393.     if (co->co_nlocals > 0)
  394.         fastlocals = ((listobject *)f->f_fastlocals)->ob_item;
  395.  
  396.     if (co->co_argcount > 0 ||
  397.         co->co_flags & (CO_VARARGS | CO_VARKEYWORDS)) {
  398.         int i;
  399.         int n = argcount;
  400.         object *kwdict = NULL;
  401.         if (co->co_flags & CO_VARKEYWORDS) {
  402.             kwdict = newmappingobject();
  403.             if (kwdict == NULL)
  404.                 goto fail;
  405.         }
  406.         if (argcount > co->co_argcount) {
  407.             if (!(co->co_flags & CO_VARARGS)) {
  408.                 err_setstr(TypeError, "too many arguments");
  409.                 goto fail;
  410.             }
  411.             n = co->co_argcount;
  412.         }
  413.         for (i = 0; i < n; i++) {
  414.             x = args[i];
  415.             INCREF(x);
  416.             SETLOCAL(i, x);
  417.         }
  418.         if (co->co_flags & CO_VARARGS) {
  419.             u = newtupleobject(argcount - n);
  420.             for (i = n; i < argcount; i++) {
  421.                 x = args[i];
  422.                 INCREF(x);
  423.                 SETTUPLEITEM(u, i-n, x);
  424.             }
  425.             SETLOCAL(co->co_argcount, u);
  426.         }
  427.         for (i = 0; i < kwcount; i++) {
  428.             object *keyword = kws[2*i];
  429.             object *value = kws[2*i + 1];
  430.             int j;
  431.             /* XXX slow -- speed up using dictionary? */
  432.             for (j = 0; j < co->co_argcount; j++) {
  433.                 object *nm = GETTUPLEITEM(co->co_varnames, j);
  434.                 if (cmpobject(keyword, nm) == 0)
  435.                     break;
  436.             }
  437.             if (j >= co->co_argcount) {
  438.                 if (kwdict == NULL) {
  439.                     err_setval(KeyError/*XXX*/, keyword);
  440.                     goto fail;
  441.                 }
  442.                 mappinginsert(kwdict, keyword, value);
  443.             }
  444.             else {
  445.                 if (GETLOCAL(j) != NULL) {
  446.                     err_setstr(TypeError,
  447.                         "keyword parameter redefined");
  448.                     goto fail;
  449.                 }
  450.                 INCREF(value);
  451.                 SETLOCAL(j, value);
  452.             }
  453.         }
  454.         if (argcount < co->co_argcount) {
  455.             int m = co->co_argcount - defcount;
  456.             for (i = argcount; i < m; i++) {
  457.                 if (GETLOCAL(i) == NULL) {
  458.                     err_setstr(TypeError,
  459.                            "not enough arguments");
  460.                     goto fail;
  461.                 }
  462.             }
  463.             if (n > m)
  464.                 i = n - m;
  465.             else
  466.                 i = 0;
  467.             for (; i < defcount; i++) {
  468.                 if (GETLOCAL(m+i) == NULL) {
  469.                     object *def = defs[i];
  470.                     INCREF(def);
  471.                     SETLOCAL(m+i, def);
  472.                 }
  473.             }
  474.         }
  475.         if (kwdict != NULL) {
  476.             i = co->co_argcount;
  477.             if (co->co_flags & CO_VARARGS)
  478.                 i++;
  479.             SETLOCAL(i, kwdict);
  480.         }
  481.         if (0) {
  482.      fail:
  483.             XDECREF(kwdict);
  484.             goto fail2;
  485.         }
  486.     }
  487.     else {
  488.         if (argcount > 0 || kwcount > 0) {
  489.             err_setstr(TypeError, "no arguments expected");
  490.  fail2:
  491.             current_frame = f->f_back;
  492.             DECREF(f);
  493.             return NULL;
  494.         }
  495.     }
  496.  
  497.     if (sys_trace != NULL) {
  498.         /* sys_trace, if defined, is a function that will
  499.            be called  on *every* entry to a code block.
  500.            Its return value, if not None, is a function that
  501.            will be called at the start of each executed line
  502.            of code.  (Actually, the function must return
  503.            itself in order to continue tracing.)
  504.            The trace functions are called with three arguments:
  505.            a pointer to the current frame, a string indicating
  506.            why the function is called, and an argument which
  507.            depends on the situation.  The global trace function
  508.            (sys.trace) is also called whenever an exception
  509.            is detected. */
  510.         if (call_trace(&sys_trace, &f->f_trace, f, "call",
  511.                    None/*XXX how to compute arguments now?*/)) {
  512.             /* Trace function raised an error */
  513.             current_frame = f->f_back;
  514.             DECREF(f);
  515.             return NULL;
  516.         }
  517.     }
  518.  
  519.     if (sys_profile != NULL) {
  520.         /* Similar for sys_profile, except it needn't return
  521.            itself and isn't called for "line" events */
  522.         if (call_trace(&sys_profile, (object**)0, f, "call",
  523.                    None/*XXX*/)) {
  524.             current_frame = f->f_back;
  525.             DECREF(f);
  526.             return NULL;
  527.         }
  528.     }
  529.  
  530.     next_instr = GETUSTRINGVALUE(f->f_code->co_code);
  531.     stack_pointer = f->f_valuestack;
  532.     
  533.     why = WHY_NOT;
  534.     err = 0;
  535.     x = None;    /* Not a reference, just anything non-NULL */
  536.     
  537.     for (;;) {
  538.         static int ticker;
  539.         
  540.         /* Do periodic things.
  541.            Doing this every time through the loop would add
  542.            too much overhead (a function call per instruction).
  543.            So we do it only every Nth instruction. */
  544.         
  545.         if (pendingfirst != pendinglast) {
  546.             if (Py_MakePendingCalls() < 0) {
  547.                 why = WHY_EXCEPTION;
  548.                 goto on_error;
  549.             }
  550.         }
  551.         
  552.         if (--ticker < 0) {
  553.             ticker = sys_checkinterval;
  554.             if (sigcheck()) {
  555.                 why = WHY_EXCEPTION;
  556.                 goto on_error;
  557.             }
  558.  
  559. #ifdef WITH_THREAD
  560.             if (interpreter_lock) {
  561.                 /* Give another thread a chance */
  562.  
  563.                 current_frame = NULL;
  564.                 release_lock(interpreter_lock);
  565.  
  566.                 /* Other threads may run now */
  567.  
  568.                 acquire_lock(interpreter_lock, 1);
  569.                 current_frame = f;
  570.             }
  571. #endif
  572.         }
  573.  
  574.         /* Extract opcode and argument */
  575.  
  576. #ifdef DEBUG
  577.         f->f_lasti = INSTR_OFFSET();
  578. #endif
  579.         
  580.         opcode = NEXTOP();
  581.         if (HAS_ARG(opcode))
  582.             oparg = NEXTARG();
  583.  
  584. #ifdef LLTRACE
  585.         /* Instruction tracing */
  586.         
  587.         if (lltrace) {
  588.             if (HAS_ARG(opcode)) {
  589.                 printf("%d: %d, %d\n",
  590.                     (int) (INSTR_OFFSET() - 3),
  591.                     opcode, oparg);
  592.             }
  593.             else {
  594.                 printf("%d: %d\n",
  595.                     (int) (INSTR_OFFSET() - 1), opcode);
  596.             }
  597.         }
  598. #endif
  599.  
  600.         if (!CHECK_STACK(3)) {
  601.             x = NULL;
  602.             break;
  603.         }
  604.  
  605.         /* Main switch on opcode */
  606.         
  607.         switch (opcode) {
  608.         
  609.         /* BEWARE!
  610.            It is essential that any operation that fails sets either
  611.            x to NULL, err to nonzero, or why to anything but WHY_NOT,
  612.            and that no operation that succeeds does this! */
  613.         
  614.         /* case STOP_CODE: this is an error! */
  615.         
  616.         case POP_TOP:
  617.             v = POP();
  618.             DECREF(v);
  619.             break;
  620.         
  621.         case ROT_TWO:
  622.             v = POP();
  623.             w = POP();
  624.             PUSH(v);
  625.             PUSH(w);
  626.             break;
  627.         
  628.         case ROT_THREE:
  629.             v = POP();
  630.             w = POP();
  631.             x = POP();
  632.             PUSH(v);
  633.             PUSH(x);
  634.             PUSH(w);
  635.             break;
  636.         
  637.         case DUP_TOP:
  638.             v = TOP();
  639.             INCREF(v);
  640.             PUSH(v);
  641.             break;
  642.         
  643.         case UNARY_POSITIVE:
  644.             v = POP();
  645.             x = pos(v);
  646.             DECREF(v);
  647.             PUSH(x);
  648.             break;
  649.         
  650.         case UNARY_NEGATIVE:
  651.             v = POP();
  652.             x = neg(v);
  653.             DECREF(v);
  654.             PUSH(x);
  655.             break;
  656.         
  657.         case UNARY_NOT:
  658.             v = POP();
  659.             x = not(v);
  660.             DECREF(v);
  661.             PUSH(x);
  662.             break;
  663.         
  664.         case UNARY_CONVERT:
  665.             v = POP();
  666.             x = reprobject(v);
  667.             DECREF(v);
  668.             PUSH(x);
  669.             break;
  670.             
  671.         case UNARY_INVERT:
  672.             v = POP();
  673.             x = invert(v);
  674.             DECREF(v);
  675.             PUSH(x);
  676.             break;
  677.         
  678.         case BINARY_MULTIPLY:
  679.             w = POP();
  680.             v = POP();
  681.             x = mul(v, w);
  682.             DECREF(v);
  683.             DECREF(w);
  684.             PUSH(x);
  685.             break;
  686.         
  687.         case BINARY_DIVIDE:
  688.             w = POP();
  689.             v = POP();
  690.             x = divide(v, w);
  691.             DECREF(v);
  692.             DECREF(w);
  693.             PUSH(x);
  694.             break;
  695.         
  696.         case BINARY_MODULO:
  697.             w = POP();
  698.             v = POP();
  699.             x = mod(v, w);
  700.             DECREF(v);
  701.             DECREF(w);
  702.             PUSH(x);
  703.             break;
  704.         
  705.         case BINARY_ADD:
  706.             w = POP();
  707.             v = POP();
  708.             x = add(v, w);
  709.             DECREF(v);
  710.             DECREF(w);
  711.             PUSH(x);
  712.             break;
  713.         
  714.         case BINARY_SUBTRACT:
  715.             w = POP();
  716.             v = POP();
  717.             x = sub(v, w);
  718.             DECREF(v);
  719.             DECREF(w);
  720.             PUSH(x);
  721.             break;
  722.         
  723.         case BINARY_SUBSCR:
  724.             w = POP();
  725.             v = POP();
  726.             x = apply_subscript(v, w);
  727.             DECREF(v);
  728.             DECREF(w);
  729.             PUSH(x);
  730.             break;
  731.         
  732.         case BINARY_LSHIFT:
  733.             w = POP();
  734.             v = POP();
  735.             x = lshift(v, w);
  736.             DECREF(v);
  737.             DECREF(w);
  738.             PUSH(x);
  739.             break;
  740.         
  741.         case BINARY_RSHIFT:
  742.             w = POP();
  743.             v = POP();
  744.             x = rshift(v, w);
  745.             DECREF(v);
  746.             DECREF(w);
  747.             PUSH(x);
  748.             break;
  749.         
  750.         case BINARY_AND:
  751.             w = POP();
  752.             v = POP();
  753.             x = and(v, w);
  754.             DECREF(v);
  755.             DECREF(w);
  756.             PUSH(x);
  757.             break;
  758.         
  759.         case BINARY_XOR:
  760.             w = POP();
  761.             v = POP();
  762.             x = xor(v, w);
  763.             DECREF(v);
  764.             DECREF(w);
  765.             PUSH(x);
  766.             break;
  767.         
  768.         case BINARY_OR:
  769.             w = POP();
  770.             v = POP();
  771.             x = or(v, w);
  772.             DECREF(v);
  773.             DECREF(w);
  774.             PUSH(x);
  775.             break;
  776.         
  777.         case SLICE+0:
  778.         case SLICE+1:
  779.         case SLICE+2:
  780.         case SLICE+3:
  781.             if ((opcode-SLICE) & 2)
  782.                 w = POP();
  783.             else
  784.                 w = NULL;
  785.             if ((opcode-SLICE) & 1)
  786.                 v = POP();
  787.             else
  788.                 v = NULL;
  789.             u = POP();
  790.             x = apply_slice(u, v, w);
  791.             DECREF(u);
  792.             XDECREF(v);
  793.             XDECREF(w);
  794.             PUSH(x);
  795.             break;
  796.         
  797.         case STORE_SLICE+0:
  798.         case STORE_SLICE+1:
  799.         case STORE_SLICE+2:
  800.         case STORE_SLICE+3:
  801.             if ((opcode-STORE_SLICE) & 2)
  802.                 w = POP();
  803.             else
  804.                 w = NULL;
  805.             if ((opcode-STORE_SLICE) & 1)
  806.                 v = POP();
  807.             else
  808.                 v = NULL;
  809.             u = POP();
  810.             t = POP();
  811.             err = assign_slice(u, v, w, t); /* u[v:w] = t */
  812.             DECREF(t);
  813.             DECREF(u);
  814.             XDECREF(v);
  815.             XDECREF(w);
  816.             break;
  817.         
  818.         case DELETE_SLICE+0:
  819.         case DELETE_SLICE+1:
  820.         case DELETE_SLICE+2:
  821.         case DELETE_SLICE+3:
  822.             if ((opcode-DELETE_SLICE) & 2)
  823.                 w = POP();
  824.             else
  825.                 w = NULL;
  826.             if ((opcode-DELETE_SLICE) & 1)
  827.                 v = POP();
  828.             else
  829.                 v = NULL;
  830.             u = POP();
  831.             err = assign_slice(u, v, w, (object *)NULL);
  832.                             /* del u[v:w] */
  833.             DECREF(u);
  834.             XDECREF(v);
  835.             XDECREF(w);
  836.             break;
  837.         
  838.         case STORE_SUBSCR:
  839.             w = POP();
  840.             v = POP();
  841.             u = POP();
  842.             /* v[w] = u */
  843.             err = assign_subscript(v, w, u);
  844.             DECREF(u);
  845.             DECREF(v);
  846.             DECREF(w);
  847.             break;
  848.         
  849.         case DELETE_SUBSCR:
  850.             w = POP();
  851.             v = POP();
  852.             /* del v[w] */
  853.             err = assign_subscript(v, w, (object *)NULL);
  854.             DECREF(v);
  855.             DECREF(w);
  856.             break;
  857.         
  858.         case PRINT_EXPR:
  859.             v = POP();
  860.             /* Print value except if procedure result */
  861.             /* Before printing, also assign to '_' */
  862.             if (v != None &&
  863.                 (err = dictinsert(f->f_builtins, "_", v)) == 0 &&
  864.                 !suppress_print) {
  865.                 flushline();
  866.                 x = sysget("stdout");
  867.                 err = writeobject(v, x, 0);
  868.                 softspace(x, 1);
  869.                 flushline();
  870.             }
  871.             DECREF(v);
  872.             break;
  873.         
  874.         case PRINT_ITEM:
  875.             v = POP();
  876.             w = sysget("stdout");
  877.             if (softspace(w, 1))
  878.                 writestring(" ", w);
  879.             err = writeobject(v, w, PRINT_RAW);
  880.             if (err == 0 && is_stringobject(v)) {
  881.                 /* XXX move into writeobject() ? */
  882.                 char *s = getstringvalue(v);
  883.                 int len = getstringsize(v);
  884.                 if (len > 0 &&
  885.                     isspace(Py_CHARMASK(s[len-1])) &&
  886.                     s[len-1] != ' ')
  887.                     softspace(w, 0);
  888.             }
  889.             DECREF(v);
  890.             break;
  891.         
  892.         case PRINT_NEWLINE:
  893.             x = sysget("stdout");
  894.             if (x == NULL)
  895.                 err_setstr(RuntimeError, "lost sys.stdout");
  896.             else {
  897.                 writestring("\n", x);
  898.                 softspace(x, 0);
  899.             }
  900.             break;
  901.         
  902.         case BREAK_LOOP:
  903.             why = WHY_BREAK;
  904.             break;
  905.  
  906.         case RAISE_VARARGS:
  907.             u = v = w = NULL;
  908.             switch (oparg) {
  909.             case 3:
  910.                 u = POP(); /* traceback */
  911.                 if (u == None) {
  912.                     DECREF(u);
  913.                     u = NULL;
  914.                 }
  915.                 else if (!PyTraceBack_Check(u)) {
  916.                     err_setstr(TypeError,
  917.                     "raise 3rd arg must be traceback or None");
  918.                     goto raise_error;
  919.                 }
  920.                 /* Fallthrough */
  921.             case 2:
  922.                 v = POP(); /* value */
  923.                 /* Fallthrough */
  924.             case 1:
  925.                 w = POP(); /* exc */
  926.                 break;
  927.             default:
  928.                 err_setstr(SystemError,
  929.                        "bad RAISE_VARARGS oparg");
  930.                 goto raise_error;
  931.             }
  932.             if (v == NULL) {
  933.                 v = None;
  934.                 INCREF(v);
  935.             }
  936.             /* A tuple is equivalent to its first element here */
  937.             while (is_tupleobject(w) && gettuplesize(w) > 0) {
  938.                 t = w;
  939.                 w = GETTUPLEITEM(w, 0);
  940.                 INCREF(w);
  941.                 DECREF(t);
  942.             }
  943.             if (is_stringobject(w)) {
  944.                 ;
  945.             } else if (is_classobject(w)) {
  946.                 if (!is_instanceobject(v)
  947.                     || !issubclass((object*)((instanceobject*)v)->in_class,
  948.                            w)) {
  949.                     err_setstr(TypeError,
  950.                            "a class exception must have a value that is an instance of the class");
  951.                     goto raise_error;
  952.                 }
  953.             } else if (is_instanceobject(w)) {
  954.                 if (v != None) {
  955.                     err_setstr(TypeError,
  956.                            "an instance exception may not have a separate value");
  957.                     goto raise_error;
  958.                 }
  959.                 else {
  960.                     DECREF(v);
  961.                     v = w;
  962.                     w = (object*) ((instanceobject*)w)->in_class;
  963.                     INCREF(w);
  964.                 }
  965.             }
  966.             else {
  967.                 err_setstr(TypeError,
  968.                     "exceptions must be strings, classes, or instances");
  969.                 goto raise_error;
  970.             }
  971.             err_restore(w, v, u);
  972.             if (u == NULL)
  973.                 why = WHY_EXCEPTION;
  974.             else
  975.                 why = WHY_RERAISE;
  976.             break;
  977.         raise_error:
  978.             XDECREF(v);
  979.             XDECREF(w);
  980.             XDECREF(u);
  981.             why = WHY_EXCEPTION;
  982.             break;
  983.         
  984.         case LOAD_LOCALS:
  985.             if ((x = f->f_locals) == NULL) {
  986.                 err_setstr(SystemError, "no locals");
  987.                 break;
  988.             }
  989.             INCREF(x);
  990.             PUSH(x);
  991.             break;
  992.         
  993.         case RETURN_VALUE:
  994.             retval = POP();
  995.             why = WHY_RETURN;
  996.             break;
  997.  
  998.         case EXEC_STMT:
  999.             w = POP();
  1000.             v = POP();
  1001.             u = POP();
  1002.             err = exec_statement(u, v, w);
  1003.             DECREF(u);
  1004.             DECREF(v);
  1005.             DECREF(w);
  1006.             break;
  1007.         
  1008.         case POP_BLOCK:
  1009.             {
  1010.                 block *b = pop_block(f);
  1011.                 while (STACK_LEVEL() > b->b_level) {
  1012.                     v = POP();
  1013.                     DECREF(v);
  1014.                 }
  1015.             }
  1016.             break;
  1017.         
  1018.         case END_FINALLY:
  1019.             v = POP();
  1020.             if (is_intobject(v)) {
  1021.                 why = (enum why_code) getintvalue(v);
  1022.                 if (why == WHY_RETURN)
  1023.                     retval = POP();
  1024.             }
  1025.             else if (is_stringobject(v) || is_classobject(v)) {
  1026.                 w = POP();
  1027.                 u = POP();
  1028.                 err_restore(v, w, u);
  1029.                 why = WHY_RERAISE;
  1030.                 break;
  1031.             }
  1032.             else if (v != None) {
  1033.                 err_setstr(SystemError,
  1034.                     "'finally' pops bad exception");
  1035.                 why = WHY_EXCEPTION;
  1036.             }
  1037.             DECREF(v);
  1038.             break;
  1039.         
  1040.         case BUILD_CLASS:
  1041.             u = POP();
  1042.             v = POP();
  1043.             w = POP();
  1044.             x = build_class(u, v, w);
  1045.             PUSH(x);
  1046.             DECREF(u);
  1047.             DECREF(v);
  1048.             DECREF(w);
  1049.             break;
  1050.         
  1051.         case STORE_NAME:
  1052.             w = GETNAMEV(oparg);
  1053.             v = POP();
  1054.             if ((x = f->f_locals) == NULL) {
  1055.                 err_setstr(SystemError, "no locals");
  1056.                 break;
  1057.             }
  1058.             u = dict2lookup(x, w);
  1059.             if (u == NULL) {
  1060.                 if (defmode != 0) {
  1061.                     if (v != None)
  1062.                         u = (object *)v->ob_type;
  1063.                     else
  1064.                         u = NULL;
  1065.                     x = newaccessobject(v, x,
  1066.                                 (typeobject *)u,
  1067.                                 defmode);
  1068.                     DECREF(v);
  1069.                     if (x == NULL)
  1070.                         break;
  1071.                     v = x;
  1072.                 }
  1073.             }
  1074.             else if (is_accessobject(u)) {
  1075.                 err = setaccessvalue(u, x, v);
  1076.                 DECREF(v);
  1077.                 break;
  1078.             }
  1079.             err = dict2insert(x, w, v);
  1080.             DECREF(v);
  1081.             break;
  1082.         
  1083.         case DELETE_NAME:
  1084.             w = GETNAMEV(oparg);
  1085.             if ((x = f->f_locals) == NULL) {
  1086.                 err_setstr(SystemError, "no locals");
  1087.                 break;
  1088.             }
  1089.             u = dict2lookup(x, w);
  1090.             if (u != NULL && is_accessobject(u)) {
  1091.                 err = setaccessvalue(u, x,
  1092.                              (object *)NULL);
  1093.                 break;
  1094.             }
  1095.             if ((err = dict2remove(x, w)) != 0)
  1096.                 err_setval(NameError, w);
  1097.             break;
  1098.  
  1099. #ifdef CASE_TOO_BIG
  1100.         default: switch (opcode) {
  1101. #endif
  1102.         
  1103.         case UNPACK_TUPLE:
  1104.             v = POP();
  1105.             if (!is_tupleobject(v)) {
  1106.                 err_setstr(TypeError, "unpack non-tuple");
  1107.                 why = WHY_EXCEPTION;
  1108.             }
  1109.             else if (gettuplesize(v) != oparg) {
  1110.                 err_setstr(ValueError,
  1111.                     "unpack tuple of wrong size");
  1112.                 why = WHY_EXCEPTION;
  1113.             }
  1114.             else {
  1115.                 if (!CHECK_STACK(oparg)) {
  1116.                     x = NULL;
  1117.                     break;
  1118.                 }
  1119.                 for (; --oparg >= 0; ) {
  1120.                     w = GETTUPLEITEM(v, oparg);
  1121.                     INCREF(w);
  1122.                     PUSH(w);
  1123.                 }
  1124.             }
  1125.             DECREF(v);
  1126.             break;
  1127.         
  1128.         case UNPACK_LIST:
  1129.             v = POP();
  1130.             if (!is_listobject(v)) {
  1131.                 err_setstr(TypeError, "unpack non-list");
  1132.                 why = WHY_EXCEPTION;
  1133.             }
  1134.             else if (getlistsize(v) != oparg) {
  1135.                 err_setstr(ValueError,
  1136.                     "unpack list of wrong size");
  1137.                 why = WHY_EXCEPTION;
  1138.             }
  1139.             else {
  1140.                 if (!CHECK_STACK(oparg)) {
  1141.                     x = NULL;
  1142.                     break;
  1143.                 }
  1144.                 for (; --oparg >= 0; ) {
  1145.                     w = getlistitem(v, oparg);
  1146.                     INCREF(w);
  1147.                     PUSH(w);
  1148.                 }
  1149.             }
  1150.             DECREF(v);
  1151.             break;
  1152.         
  1153.         case STORE_ATTR:
  1154.             w = GETNAMEV(oparg);
  1155.             v = POP();
  1156.             u = POP();
  1157.             err = setattro(v, w, u); /* v.w = u */
  1158.             DECREF(v);
  1159.             DECREF(u);
  1160.             break;
  1161.         
  1162.         case DELETE_ATTR:
  1163.             w = GETNAMEV(oparg);
  1164.             v = POP();
  1165.             err = setattro(v, w, (object *)NULL); /* del v.w */
  1166.             DECREF(v);
  1167.             break;
  1168.         
  1169.         case STORE_GLOBAL:
  1170.             w = GETNAMEV(oparg);
  1171.             v = POP();
  1172.             if (f->f_locals != NULL) {
  1173.                 u = dict2lookup(f->f_locals, w);
  1174.                 if (u != NULL && is_accessobject(u)) {
  1175.                     err = setaccessvalue(u, f->f_globals,
  1176.                                  v);
  1177.                     DECREF(v);
  1178.                     break;
  1179.                 }
  1180.             }
  1181.             err = dict2insert(f->f_globals, w, v);
  1182.             DECREF(v);
  1183.             break;
  1184.         
  1185.         case DELETE_GLOBAL:
  1186.             w = GETNAMEV(oparg);
  1187.             if (f->f_locals != NULL) {
  1188.                 u = dict2lookup(f->f_locals, w);
  1189.                 if (u != NULL && is_accessobject(u)) {
  1190.                     err = setaccessvalue(u, f->f_globals,
  1191.                                  (object *)NULL);
  1192.                     break;
  1193.                 }
  1194.             }
  1195.             if ((err = dict2remove(f->f_globals, w)) != 0)
  1196.                 err_setval(NameError, w);
  1197.             break;
  1198.         
  1199.         case LOAD_CONST:
  1200.             x = GETCONST(oparg);
  1201.             INCREF(x);
  1202.             PUSH(x);
  1203.             break;
  1204.         
  1205.         case LOAD_NAME:
  1206.             w = GETNAMEV(oparg);
  1207.             if ((x = f->f_locals) == NULL) {
  1208.                 err_setstr(SystemError, "no locals");
  1209.                 break;
  1210.             }
  1211.             x = dict2lookup(x, w);
  1212.             if (x == NULL) {
  1213.                 err_clear();
  1214.                 x = dict2lookup(f->f_globals, w);
  1215.                 if (x == NULL) {
  1216.                     err_clear();
  1217.                     x = dict2lookup(f->f_builtins, w);
  1218.                     if (x == NULL) {
  1219.                         err_setval(NameError, w);
  1220.                         break;
  1221.                     }
  1222.                 }
  1223.             }
  1224.             if (is_accessobject(x)) {
  1225.                 x = getaccessvalue(x, f->f_globals /* XXX */);
  1226.                 if (x == NULL)
  1227.                     break;
  1228.             }
  1229.             else
  1230.                 INCREF(x);
  1231.             PUSH(x);
  1232.             break;
  1233.         
  1234.         case LOAD_GLOBAL:
  1235.             w = GETNAMEV(oparg);
  1236.             x = dict2lookup(f->f_globals, w);
  1237.             if (x == NULL) {
  1238.                 err_clear();
  1239.                 x = dict2lookup(f->f_builtins, w);
  1240.                 if (x == NULL) {
  1241.                     err_setval(NameError, w);
  1242.                     break;
  1243.                 }
  1244.             }
  1245.             if (is_accessobject(x)) {
  1246.                 x = getaccessvalue(x, f->f_globals);
  1247.                 if (x == NULL)
  1248.                     break;
  1249.             }
  1250.             else
  1251.                 INCREF(x);
  1252.             PUSH(x);
  1253.             break;
  1254.  
  1255. #if 0
  1256.         case LOAD_LOCAL:
  1257.             w = GETNAMEV(oparg);
  1258.             if ((x = f->f_locals) == NULL) {
  1259.                 err_setstr(SystemError, "no locals");
  1260.                 break;
  1261.             }
  1262.             x = dict2lookup(x, w);
  1263.             if (x == NULL) {
  1264.                 err_setval(NameError, w);
  1265.                 break;
  1266.             }
  1267.             if (is_accessobject(x)) {
  1268.                 x = getaccessvalue(x, f->f_locals);
  1269.                 if (x == NULL)
  1270.                     break;
  1271.             }
  1272.             else
  1273.                 INCREF(x);
  1274.             PUSH(x);
  1275.             break;
  1276. #endif
  1277.  
  1278.         case LOAD_FAST:
  1279.             x = GETLOCAL(oparg);
  1280.             if (x == NULL) {
  1281.                 err_setval(NameError,
  1282.                        gettupleitem(co->co_varnames,
  1283.                             oparg));
  1284.                 break;
  1285.             }
  1286.             if (is_accessobject(x)) {
  1287.                 x = getaccessvalue(x, f->f_locals);
  1288.                 if (x == NULL)
  1289.                     break;
  1290.             }
  1291.             else
  1292.                 INCREF(x);
  1293.             PUSH(x);
  1294.             break;
  1295.  
  1296.         case STORE_FAST:
  1297.             v = POP();
  1298.             w = GETLOCAL(oparg);
  1299.             if (w != NULL && is_accessobject(w)) {
  1300.                 err = setaccessvalue(w, f->f_locals, v);
  1301.                 DECREF(v);
  1302.                 break;
  1303.             }
  1304.             SETLOCAL(oparg, v);
  1305.             break;
  1306.  
  1307.         case DELETE_FAST:
  1308.             x = GETLOCAL(oparg);
  1309.             if (x == NULL) {
  1310.                 err_setval(NameError,
  1311.                        gettupleitem(co->co_varnames,
  1312.                             oparg));
  1313.                 break;
  1314.             }
  1315.             if (is_accessobject(x)) {
  1316.                 err = setaccessvalue(x, f->f_locals,
  1317.                              (object *)NULL);
  1318.                 break;
  1319.             }
  1320.             SETLOCAL(oparg, NULL);
  1321.             break;
  1322.         
  1323.         case BUILD_TUPLE:
  1324.             x = newtupleobject(oparg);
  1325.             if (x != NULL) {
  1326.                 for (; --oparg >= 0;) {
  1327.                     w = POP();
  1328.                     SETTUPLEITEM(x, oparg, w);
  1329.                 }
  1330.                 PUSH(x);
  1331.             }
  1332.             break;
  1333.         
  1334.         case BUILD_LIST:
  1335.             x =  newlistobject(oparg);
  1336.             if (x != NULL) {
  1337.                 for (; --oparg >= 0;) {
  1338.                     w = POP();
  1339.                     err = setlistitem(x, oparg, w);
  1340.                     if (err != 0)
  1341.                         break;
  1342.                 }
  1343.                 PUSH(x);
  1344.             }
  1345.             break;
  1346.         
  1347.         case BUILD_MAP:
  1348.             x = newdictobject();
  1349.             PUSH(x);
  1350.             break;
  1351.         
  1352.         case LOAD_ATTR:
  1353.             w = GETNAMEV(oparg);
  1354.             v = POP();
  1355.             x = getattro(v, w);
  1356.             DECREF(v);
  1357.             PUSH(x);
  1358.             break;
  1359.         
  1360.         case COMPARE_OP:
  1361.             w = POP();
  1362.             v = POP();
  1363.             x = cmp_outcome(oparg, v, w);
  1364.             DECREF(v);
  1365.             DECREF(w);
  1366.             PUSH(x);
  1367.             break;
  1368.         
  1369.         case IMPORT_NAME:
  1370.             w = GETNAMEV(oparg);
  1371.             x = dictlookup(f->f_builtins, "__import__");
  1372.             if (x == NULL) {
  1373.                 err_setstr(ImportError,
  1374.                        "__import__ not found");
  1375.                 break;
  1376.             }
  1377.             if (is_methodobject(x)) {
  1378.                 u = None;
  1379.                 INCREF(u);
  1380.             }
  1381.             else {
  1382.                 u = find_from_args(f, INSTR_OFFSET());
  1383.                 if (u == NULL) {
  1384.                     x = u;
  1385.                     break;
  1386.                 }
  1387.             }
  1388.             w = mkvalue("(OOOO)",
  1389.                     w,
  1390.                     f->f_globals,
  1391.                     f->f_locals == NULL ? None : f->f_locals,
  1392.                     u);
  1393.             DECREF(u);
  1394.             if (w == NULL) {
  1395.                 x = NULL;
  1396.                 break;
  1397.             }
  1398.             x = call_object(x, w);
  1399.             DECREF(w);
  1400.             PUSH(x);
  1401.             break;
  1402.         
  1403.         case IMPORT_FROM:
  1404.             w = GETNAMEV(oparg);
  1405.             v = TOP();
  1406.             fast_2_locals(f);
  1407.             if ((x = f->f_locals) == NULL) {
  1408.                 err_setstr(SystemError, "no locals");
  1409.                 break;
  1410.             }
  1411.             err = import_from(x, v, w);
  1412.             locals_2_fast(f, 0);
  1413.             break;
  1414.  
  1415.         case ACCESS_MODE:
  1416.             v = POP();
  1417.             w = GETNAMEV(oparg);
  1418.             if (getstringvalue(w)[0] == '*')
  1419.                 defmode = getintvalue(v);
  1420.             else
  1421.                 err = access_statement(w, v, f);
  1422.             DECREF(v);
  1423.             break;
  1424.         
  1425.         case JUMP_FORWARD:
  1426.             JUMPBY(oparg);
  1427.             break;
  1428.         
  1429.         case JUMP_IF_FALSE:
  1430.             err = testbool(TOP());
  1431.             if (err > 0)
  1432.                 err = 0;
  1433.             else if (err == 0)
  1434.                 JUMPBY(oparg);
  1435.             break;
  1436.         
  1437.         case JUMP_IF_TRUE:
  1438.             err = testbool(TOP());
  1439.             if (err > 0) {
  1440.                 err = 0;
  1441.                 JUMPBY(oparg);
  1442.             }
  1443.             break;
  1444.         
  1445.         case JUMP_ABSOLUTE:
  1446.             JUMPTO(oparg);
  1447.             break;
  1448.         
  1449.         case FOR_LOOP:
  1450.             /* for v in s: ...
  1451.                On entry: stack contains s, i.
  1452.                On exit: stack contains s, i+1, s[i];
  1453.                but if loop exhausted:
  1454.                    s, i are popped, and we jump */
  1455.             w = POP(); /* Loop index */
  1456.             v = POP(); /* Sequence object */
  1457.             u = loop_subscript(v, w);
  1458.             if (u != NULL) {
  1459.                 PUSH(v);
  1460.                 x = newintobject(getintvalue(w)+1);
  1461.                 PUSH(x);
  1462.                 DECREF(w);
  1463.                 PUSH(u);
  1464.             }
  1465.             else {
  1466.                 DECREF(v);
  1467.                 DECREF(w);
  1468.                 /* A NULL can mean "s exhausted"
  1469.                    but also an error: */
  1470.                 if (err_occurred())
  1471.                     why = WHY_EXCEPTION;
  1472.                 else
  1473.                     JUMPBY(oparg);
  1474.             }
  1475.             break;
  1476.         
  1477.         case SETUP_LOOP:
  1478.         case SETUP_EXCEPT:
  1479.         case SETUP_FINALLY:
  1480.             setup_block(f, opcode, INSTR_OFFSET() + oparg,
  1481.                         STACK_LEVEL());
  1482.             break;
  1483.         
  1484.         case SET_LINENO:
  1485. #ifdef LLTRACE
  1486.             if (lltrace)
  1487.                 printf("--- %s:%d \n", filename, oparg);
  1488. #endif
  1489.             f->f_lineno = oparg;
  1490.             if (f->f_trace != NULL) {
  1491.                 /* Trace each line of code reached */
  1492.                 f->f_lasti = INSTR_OFFSET();
  1493.                 err = call_trace(&f->f_trace, &f->f_trace,
  1494.                          f, "line", None);
  1495.             }
  1496.             break;
  1497.  
  1498.         case CALL_FUNCTION:
  1499.         {
  1500.             int na = oparg & 0xff;
  1501.             int nk = (oparg>>8) & 0xff;
  1502.             int n = na + 2*nk;
  1503.             object **pfunc = stack_pointer - n - 1;
  1504.             object *func = *pfunc;
  1505.             object *self = NULL;
  1506.             object *class = NULL;
  1507.             f->f_lasti = INSTR_OFFSET() - 3; /* For tracing */
  1508.             if (is_instancemethodobject(func)) {
  1509.                 self = instancemethodgetself(func);
  1510.                 class = instancemethodgetclass(func);
  1511.                 func = instancemethodgetfunc(func);
  1512.                 INCREF(func);
  1513.                 if (self != NULL) {
  1514.                     INCREF(self);
  1515.                     DECREF(*pfunc);
  1516.                     *pfunc = self;
  1517.                     na++;
  1518.                     n++;
  1519.                 }
  1520.                 else {
  1521.                     /* Unbound methods must be
  1522.                        called with an instance of
  1523.                        the class (or a derived
  1524.                        class) as first argument */
  1525.                     if (na > 0 &&
  1526.                         (self = stack_pointer[-n])
  1527.                          != NULL &&
  1528.                         is_instanceobject(self) &&
  1529.                         issubclass(
  1530.                             (object *)
  1531.                             (((instanceobject *)self)
  1532.                              ->in_class),
  1533.                             class))
  1534.                         /* Handy-dandy */ ;
  1535.                     else {
  1536.                         err_setstr(TypeError,
  1537.        "unbound method must be called with class instance 1st argument");
  1538.                         return NULL;
  1539.                     }
  1540.                 }
  1541.             }
  1542.             else
  1543.                 INCREF(func);
  1544.             if (is_funcobject(func)) {
  1545.                 object *co = getfunccode(func);
  1546.                 object *globals = getfuncglobals(func);
  1547.                 object *argdefs = PyFunction_GetDefaults(func);
  1548.                 object **d;
  1549.                 int nd;
  1550.                 if (argdefs != NULL) {
  1551.                     d = &GETTUPLEITEM(argdefs, 0);
  1552.                     nd = ((tupleobject *)argdefs)->ob_size;
  1553.                 }
  1554.                 else {
  1555.                     d = NULL;
  1556.                     nd = 0;
  1557.                 }
  1558.                 x = eval_code2(
  1559.                     (codeobject *)co,
  1560.                     globals, (object *)NULL,
  1561.                     stack_pointer-n, na,
  1562.                     stack_pointer-2*nk, nk,
  1563.                     d, nd,
  1564.                     class);
  1565.             }
  1566.             else {
  1567.                 object *args = newtupleobject(na);
  1568.                 object *kwdict = NULL;
  1569.                 if (args == NULL) {
  1570.                     x = NULL;
  1571.                     break;
  1572.                 }
  1573.                 if (nk > 0) {
  1574.                     kwdict = newdictobject();
  1575.                     if (kwdict == NULL) {
  1576.                         x = NULL;
  1577.                         break;
  1578.                     }
  1579.                     err = 0;
  1580.                     while (--nk >= 0) {
  1581.                         object *value = POP();
  1582.                         object *key = POP();
  1583.                         err = mappinginsert(
  1584.                             kwdict, key, value);
  1585.                         if (err) {
  1586.                             DECREF(key);
  1587.                             DECREF(value);
  1588.                             break;
  1589.                         }
  1590.                     }
  1591.                     if (err) {
  1592.                         DECREF(args);
  1593.                         DECREF(kwdict);
  1594.                         break;
  1595.                     }
  1596.                 }
  1597.                 while (--na >= 0) {
  1598.                     w = POP();
  1599.                     SETTUPLEITEM(args, na, w);
  1600.                 }
  1601.                 x = PyEval_CallObjectWithKeywords(
  1602.                     func, args, kwdict);
  1603.                 DECREF(args);
  1604.                 XDECREF(kwdict);
  1605.             }
  1606.             DECREF(func);
  1607.             while (stack_pointer > pfunc) {
  1608.                 w = POP();
  1609.                 DECREF(w);
  1610.             }
  1611.             PUSH(x);
  1612.             break;
  1613.         }
  1614.         
  1615.         case MAKE_FUNCTION:
  1616.             v = POP(); /* code object */
  1617.             x = newfuncobject(v, f->f_globals);
  1618.             DECREF(v);
  1619.             /* XXX Maybe this should be a separate opcode? */
  1620.             if (x != NULL && oparg > 0) {
  1621.                 v = newtupleobject(oparg);
  1622.                 if (v == NULL) {
  1623.                     DECREF(x);
  1624.                     x = NULL;
  1625.                     break;
  1626.                 }
  1627.                 while (--oparg >= 0) {
  1628.                     w = POP();
  1629.                     SETTUPLEITEM(v, oparg, w);
  1630.                 }
  1631.                 err = PyFunction_SetDefaults(x, v);
  1632.                 DECREF(v);
  1633.             }
  1634.             PUSH(x);
  1635.             break;
  1636.         
  1637.         default:
  1638.             fprintf(stderr,
  1639.                 "XXX lineno: %d, opcode: %d\n",
  1640.                 f->f_lineno, opcode);
  1641.             err_setstr(SystemError, "unknown opcode");
  1642.             why = WHY_EXCEPTION;
  1643.             break;
  1644.  
  1645. #ifdef CASE_TOO_BIG
  1646.         }
  1647. #endif
  1648.  
  1649.         } /* switch */
  1650.  
  1651.         on_error:
  1652.         
  1653.         /* Quickly continue if no error occurred */
  1654.         
  1655.         if (why == WHY_NOT) {
  1656.             if (err == 0 && x != NULL) {
  1657. #ifdef CHECKEXC
  1658.                 if (err_occurred())
  1659.                     fprintf(stderr,
  1660.                         "XXX undetected error\n");
  1661.                 else
  1662. #endif
  1663.                     continue; /* Normal, fast path */
  1664.             }
  1665.             why = WHY_EXCEPTION;
  1666.             x = None;
  1667.             err = 0;
  1668.         }
  1669.  
  1670. #ifdef CHECKEXC
  1671.         /* Double-check exception status */
  1672.         
  1673.         if (why == WHY_EXCEPTION || why == WHY_RERAISE) {
  1674.             if (!err_occurred()) {
  1675.                 fprintf(stderr, "XXX ghost error\n");
  1676.                 err_setstr(SystemError, "ghost error");
  1677.                 why = WHY_EXCEPTION;
  1678.             }
  1679.         }
  1680.         else {
  1681.             if (err_occurred()) {
  1682.                 fprintf(stderr,
  1683.                     "XXX undetected error (why=%d)\n",
  1684.                     why);
  1685.                 why = WHY_EXCEPTION;
  1686.             }
  1687.         }
  1688. #endif
  1689.  
  1690.         /* Log traceback info if this is a real exception */
  1691.         
  1692.         if (why == WHY_EXCEPTION) {
  1693.             f->f_lasti = INSTR_OFFSET() - 1;
  1694.             if (HAS_ARG(opcode))
  1695.                 f->f_lasti -= 2;
  1696.             tb_here(f);
  1697.  
  1698.             if (f->f_trace)
  1699.                 call_exc_trace(&f->f_trace, &f->f_trace, f);
  1700.             if (sys_profile)
  1701.                 call_exc_trace(&sys_profile, (object**)0, f);
  1702.         }
  1703.         
  1704.         /* For the rest, treat WHY_RERAISE as WHY_EXCEPTION */
  1705.         
  1706.         if (why == WHY_RERAISE)
  1707.             why = WHY_EXCEPTION;
  1708.  
  1709.         /* Unwind stacks if a (pseudo) exception occurred */
  1710.         
  1711.         while (why != WHY_NOT && f->f_iblock > 0) {
  1712.             block *b = pop_block(f);
  1713.             while (STACK_LEVEL() > b->b_level) {
  1714.                 v = POP();
  1715.                 XDECREF(v);
  1716.             }
  1717.             if (b->b_type == SETUP_LOOP && why == WHY_BREAK) {
  1718.                 why = WHY_NOT;
  1719.                 JUMPTO(b->b_handler);
  1720.                 break;
  1721.             }
  1722.             if (b->b_type == SETUP_FINALLY ||
  1723.                     b->b_type == SETUP_EXCEPT &&
  1724.                     why == WHY_EXCEPTION) {
  1725.                 if (why == WHY_EXCEPTION) {
  1726.                     object *exc, *val, *tb;
  1727.                     err_fetch(&exc, &val, &tb);
  1728.                     if (val == NULL) {
  1729.                         val = None;
  1730.                         INCREF(val);
  1731.                     }
  1732.                     /* Make the raw exception data
  1733.                        available to the handler,
  1734.                        so a program can emulate the
  1735.                        Python main loop.  Don't do
  1736.                        this for 'finally'. */
  1737.                     if (b->b_type == SETUP_EXCEPT) {
  1738.                         sysset("exc_traceback", tb);
  1739.                         sysset("exc_value", val);
  1740.                         sysset("exc_type", exc);
  1741.                     }
  1742.                     PUSH(tb);
  1743.                     PUSH(val);
  1744.                     PUSH(exc);
  1745.                 }
  1746.                 else {
  1747.                     if (why == WHY_RETURN)
  1748.                         PUSH(retval);
  1749.                     v = newintobject((long)why);
  1750.                     PUSH(v);
  1751.                 }
  1752.                 why = WHY_NOT;
  1753.                 JUMPTO(b->b_handler);
  1754.                 break;
  1755.             }
  1756.         } /* unwind stack */
  1757.  
  1758.         /* End the loop if we still have an error (or return) */
  1759.         
  1760.         if (why != WHY_NOT)
  1761.             break;
  1762.         
  1763.     } /* main loop */
  1764.     
  1765.     /* Pop remaining stack entries */
  1766.     
  1767.     while (!EMPTY()) {
  1768.         v = POP();
  1769.         XDECREF(v);
  1770.     }
  1771.     
  1772.     if (why != WHY_RETURN)
  1773.         retval = NULL;
  1774.     
  1775.     if (f->f_trace) {
  1776.         if (why == WHY_RETURN) {
  1777.             if (call_trace(&f->f_trace, &f->f_trace, f,
  1778.                        "return", retval)) {
  1779.                 XDECREF(retval);
  1780.                 retval = NULL;
  1781.                 why = WHY_EXCEPTION;
  1782.             }
  1783.         }
  1784.     }
  1785.     
  1786.     if (sys_profile && why == WHY_RETURN) {
  1787.         if (call_trace(&sys_profile, (object**)0,
  1788.                    f, "return", retval)) {
  1789.             XDECREF(retval);
  1790.             retval = NULL;
  1791.             why = WHY_EXCEPTION;
  1792.         }
  1793.     }
  1794.     
  1795.     /* Restore previous frame and release the current one */
  1796.  
  1797.     current_frame = f->f_back;
  1798.     DECREF(f);
  1799.     
  1800.     return retval;
  1801. }
  1802.  
  1803. #ifdef LLTRACE
  1804. static int
  1805. prtrace(v, str)
  1806.     object *v;
  1807.     char *str;
  1808. {
  1809.     printf("%s ", str);
  1810.     if (printobject(v, stdout, 0) != 0)
  1811.         err_clear(); /* Don't know what else to do */
  1812.     printf("\n");
  1813. }
  1814. #endif
  1815.  
  1816. static void
  1817. call_exc_trace(p_trace, p_newtrace, f)
  1818.     object **p_trace, **p_newtrace;
  1819.     frameobject *f;
  1820. {
  1821.     object *type, *value, *traceback, *arg;
  1822.     int err;
  1823.     err_fetch(&type, &value, &traceback);
  1824.     if (value == NULL) {
  1825.         value = None;
  1826.         INCREF(value);
  1827.     }
  1828.     arg = mkvalue("(OOO)", type, value, traceback);
  1829.     if (arg == NULL) {
  1830.         err_restore(type, value, traceback);
  1831.         return;
  1832.     }
  1833.     err = call_trace(p_trace, p_newtrace, f, "exception", arg);
  1834.     DECREF(arg);
  1835.     if (err == 0)
  1836.         err_restore(type, value, traceback);
  1837.     else {
  1838.         XDECREF(type);
  1839.         XDECREF(value);
  1840.         XDECREF(traceback);
  1841.     }
  1842. }
  1843.  
  1844. static int
  1845. call_trace(p_trace, p_newtrace, f, msg, arg)
  1846.     object **p_trace; /* in/out; may not be NULL;
  1847.                  may not point to NULL variable initially */
  1848.     object **p_newtrace; /* in/out; may be NULL;
  1849.                 may point to NULL variable;
  1850.                 may be same variable as p_newtrace */
  1851.     frameobject *f;
  1852.     char *msg;
  1853.     object *arg;
  1854. {
  1855.     object *args, *what;
  1856.     object *res = NULL;
  1857.     static int tracing = 0;
  1858.     
  1859.     if (tracing) {
  1860.         /* Don't do recursive traces */
  1861.         if (p_newtrace) {
  1862.             XDECREF(*p_newtrace);
  1863.             *p_newtrace = NULL;
  1864.         }
  1865.         return 0;
  1866.     }
  1867.     
  1868.     args = newtupleobject(3);
  1869.     if (args == NULL)
  1870.         goto cleanup;
  1871.     what = newstringobject(msg);
  1872.     if (what == NULL)
  1873.         goto cleanup;
  1874.     INCREF(f);
  1875.     SETTUPLEITEM(args, 0, (object *)f);
  1876.     SETTUPLEITEM(args, 1, what);
  1877.     if (arg == NULL)
  1878.         arg = None;
  1879.     INCREF(arg);
  1880.     SETTUPLEITEM(args, 2, arg);
  1881.     tracing++;
  1882.     fast_2_locals(f);
  1883.     res = call_object(*p_trace, args); /* May clear *p_trace! */
  1884.     locals_2_fast(f, 1);
  1885.     tracing--;
  1886.  cleanup:
  1887.     XDECREF(args);
  1888.     if (res == NULL) {
  1889.         /* The trace proc raised an exception */
  1890.         tb_here(f);
  1891.         XDECREF(*p_trace);
  1892.         *p_trace = NULL;
  1893.         if (p_newtrace) {
  1894.             XDECREF(*p_newtrace);
  1895.             *p_newtrace = NULL;
  1896.         }
  1897.         return -1;
  1898.     }
  1899.     else {
  1900.         if (p_newtrace) {
  1901.             XDECREF(*p_newtrace);
  1902.             if (res == None)
  1903.                 *p_newtrace = NULL;
  1904.             else {
  1905.                 INCREF(res);
  1906.                 *p_newtrace = res;
  1907.             }
  1908.         }
  1909.         DECREF(res);
  1910.         return 0;
  1911.     }
  1912. }
  1913.  
  1914. object *
  1915. getbuiltins()
  1916. {
  1917.     if (current_frame == NULL)
  1918.         return getbuiltinmod();
  1919.     else
  1920.         return current_frame->f_builtins;
  1921. }
  1922.  
  1923. object *
  1924. getlocals()
  1925. {
  1926.     if (current_frame == NULL)
  1927.         return NULL;
  1928.     fast_2_locals(current_frame);
  1929.     return current_frame->f_locals;
  1930. }
  1931.  
  1932. object *
  1933. getglobals()
  1934. {
  1935.     if (current_frame == NULL)
  1936.         return NULL;
  1937.     else
  1938.         return current_frame->f_globals;
  1939. }
  1940.  
  1941. object *
  1942. getowner()
  1943. {
  1944.     if (current_frame == NULL)
  1945.         return NULL;
  1946.     else
  1947.         return current_frame->f_owner;
  1948. }
  1949.  
  1950. object *
  1951. getframe()
  1952. {
  1953.     return (object *)current_frame;
  1954. }
  1955.  
  1956. int
  1957. getrestricted()
  1958. {
  1959.     return current_frame == NULL ? 0 : current_frame->f_restricted;
  1960. }
  1961.  
  1962. void
  1963. flushline()
  1964. {
  1965.     object *f = sysget("stdout");
  1966.     if (softspace(f, 0))
  1967.         writestring("\n", f);
  1968. }
  1969.  
  1970.  
  1971. #define BINOP(opname, ropname, thisfunc) \
  1972.     if (!is_instanceobject(v) && !is_instanceobject(w)) \
  1973.         ; \
  1974.     else \
  1975.         return instancebinop(v, w, opname, ropname, thisfunc)
  1976.  
  1977.  
  1978. static object *
  1979. or(v, w)
  1980.     object *v, *w;
  1981. {
  1982.     BINOP("__or__", "__ror__", or);
  1983.     if (v->ob_type->tp_as_number != NULL) {
  1984.         object *x;
  1985.         object * (*f) FPROTO((object *, object *));
  1986.         if (coerce(&v, &w) != 0)
  1987.             return NULL;
  1988.         if ((f = v->ob_type->tp_as_number->nb_or) != NULL)
  1989.             x = (*f)(v, w);
  1990.         DECREF(v);
  1991.         DECREF(w);
  1992.         if (f != NULL)
  1993.             return x;
  1994.     }
  1995.     err_setstr(TypeError, "bad operand type(s) for |");
  1996.     return NULL;
  1997. }
  1998.  
  1999. static object *
  2000. xor(v, w)
  2001.     object *v, *w;
  2002. {
  2003.     BINOP("__xor__", "__rxor__", xor);
  2004.     if (v->ob_type->tp_as_number != NULL) {
  2005.         object *x;
  2006.         object * (*f) FPROTO((object *, object *));
  2007.         if (coerce(&v, &w) != 0)
  2008.             return NULL;
  2009.         if ((f = v->ob_type->tp_as_number->nb_xor) != NULL)
  2010.             x = (*f)(v, w);
  2011.         DECREF(v);
  2012.         DECREF(w);
  2013.         if (f != NULL)
  2014.             return x;
  2015.     }
  2016.     err_setstr(TypeError, "bad operand type(s) for ^");
  2017.     return NULL;
  2018. }
  2019.  
  2020. static object *
  2021. and(v, w)
  2022.     object *v, *w;
  2023. {
  2024.     BINOP("__and__", "__rand__", and);
  2025.     if (v->ob_type->tp_as_number != NULL) {
  2026.         object *x;
  2027.         object * (*f) FPROTO((object *, object *));
  2028.         if (coerce(&v, &w) != 0)
  2029.             return NULL;
  2030.         if ((f = v->ob_type->tp_as_number->nb_and) != NULL)
  2031.             x = (*f)(v, w);
  2032.         DECREF(v);
  2033.         DECREF(w);
  2034.         if (f != NULL)
  2035.             return x;
  2036.     }
  2037.     err_setstr(TypeError, "bad operand type(s) for &");
  2038.     return NULL;
  2039. }
  2040.  
  2041. static object *
  2042. lshift(v, w)
  2043.     object *v, *w;
  2044. {
  2045.     BINOP("__lshift__", "__rlshift__", lshift);
  2046.     if (v->ob_type->tp_as_number != NULL) {
  2047.         object *x;
  2048.         object * (*f) FPROTO((object *, object *));
  2049.         if (coerce(&v, &w) != 0)
  2050.             return NULL;
  2051.         if ((f = v->ob_type->tp_as_number->nb_lshift) != NULL)
  2052.             x = (*f)(v, w);
  2053.         DECREF(v);
  2054.         DECREF(w);
  2055.         if (f != NULL)
  2056.             return x;
  2057.     }
  2058.     err_setstr(TypeError, "bad operand type(s) for <<");
  2059.     return NULL;
  2060. }
  2061.  
  2062. static object *
  2063. rshift(v, w)
  2064.     object *v, *w;
  2065. {
  2066.     BINOP("__rshift__", "__rrshift__", rshift);
  2067.     if (v->ob_type->tp_as_number != NULL) {
  2068.         object *x;
  2069.         object * (*f) FPROTO((object *, object *));
  2070.         if (coerce(&v, &w) != 0)
  2071.             return NULL;
  2072.         if ((f = v->ob_type->tp_as_number->nb_rshift) != NULL)
  2073.             x = (*f)(v, w);
  2074.         DECREF(v);
  2075.         DECREF(w);
  2076.         if (f != NULL)
  2077.             return x;
  2078.     }
  2079.     err_setstr(TypeError, "bad operand type(s) for >>");
  2080.     return NULL;
  2081. }
  2082.  
  2083. static object *
  2084. add(v, w)
  2085.     object *v, *w;
  2086. {
  2087.     BINOP("__add__", "__radd__", add);
  2088.     if (v->ob_type->tp_as_sequence != NULL)
  2089.         return (*v->ob_type->tp_as_sequence->sq_concat)(v, w);
  2090.     else if (v->ob_type->tp_as_number != NULL) {
  2091.         object *x;
  2092.         if (coerce(&v, &w) != 0)
  2093.             return NULL;
  2094.         x = (*v->ob_type->tp_as_number->nb_add)(v, w);
  2095.         DECREF(v);
  2096.         DECREF(w);
  2097.         return x;
  2098.     }
  2099.     err_setstr(TypeError, "bad operand type(s) for +");
  2100.     return NULL;
  2101. }
  2102.  
  2103. static object *
  2104. sub(v, w)
  2105.     object *v, *w;
  2106. {
  2107.     BINOP("__sub__", "__rsub__", sub);
  2108.     if (v->ob_type->tp_as_number != NULL) {
  2109.         object *x;
  2110.         if (coerce(&v, &w) != 0)
  2111.             return NULL;
  2112.         x = (*v->ob_type->tp_as_number->nb_subtract)(v, w);
  2113.         DECREF(v);
  2114.         DECREF(w);
  2115.         return x;
  2116.     }
  2117.     err_setstr(TypeError, "bad operand type(s) for -");
  2118.     return NULL;
  2119. }
  2120.  
  2121. static object *
  2122. mul(v, w)
  2123.     object *v, *w;
  2124. {
  2125.     typeobject *tp;
  2126.     tp = v->ob_type;
  2127.     BINOP("__mul__", "__rmul__", mul);
  2128.     if (tp->tp_as_number != NULL &&
  2129.         w->ob_type->tp_as_sequence != NULL &&
  2130.         !is_instanceobject(v)) {
  2131.         /* number*sequence -- swap v and w */
  2132.         object *tmp = v;
  2133.         v = w;
  2134.         w = tmp;
  2135.         tp = v->ob_type;
  2136.     }
  2137.     if (tp->tp_as_number != NULL) {
  2138.         object *x;
  2139.         if (is_instanceobject(v)) {
  2140.             /* Instances of user-defined classes get their
  2141.                other argument uncoerced, so they may
  2142.                implement sequence*number as well as
  2143.                number*number. */
  2144.             INCREF(v);
  2145.             INCREF(w);
  2146.         }
  2147.         else if (coerce(&v, &w) != 0)
  2148.             return NULL;
  2149.         x = (*v->ob_type->tp_as_number->nb_multiply)(v, w);
  2150.         DECREF(v);
  2151.         DECREF(w);
  2152.         return x;
  2153.     }
  2154.     if (tp->tp_as_sequence != NULL) {
  2155.         if (!is_intobject(w)) {
  2156.             err_setstr(TypeError,
  2157.                 "can't multiply sequence with non-int");
  2158.             return NULL;
  2159.         }
  2160.         return (*tp->tp_as_sequence->sq_repeat)
  2161.                         (v, (int)getintvalue(w));
  2162.     }
  2163.     err_setstr(TypeError, "bad operand type(s) for *");
  2164.     return NULL;
  2165. }
  2166.  
  2167. static object *
  2168. divide(v, w)
  2169.     object *v, *w;
  2170. {
  2171.     BINOP("__div__", "__rdiv__", divide);
  2172.     if (v->ob_type->tp_as_number != NULL) {
  2173.         object *x;
  2174.         if (coerce(&v, &w) != 0)
  2175.             return NULL;
  2176.         x = (*v->ob_type->tp_as_number->nb_divide)(v, w);
  2177.         DECREF(v);
  2178.         DECREF(w);
  2179.         return x;
  2180.     }
  2181.     err_setstr(TypeError, "bad operand type(s) for /");
  2182.     return NULL;
  2183. }
  2184.  
  2185. static object *
  2186. mod(v, w)
  2187.     object *v, *w;
  2188. {
  2189.     if (is_stringobject(v)) {
  2190.         return formatstring(v, w);
  2191.     }
  2192.     BINOP("__mod__", "__rmod__", mod);
  2193.     if (v->ob_type->tp_as_number != NULL) {
  2194.         object *x;
  2195.         if (coerce(&v, &w) != 0)
  2196.             return NULL;
  2197.         x = (*v->ob_type->tp_as_number->nb_remainder)(v, w);
  2198.         DECREF(v);
  2199.         DECREF(w);
  2200.         return x;
  2201.     }
  2202.     err_setstr(TypeError, "bad operand type(s) for %");
  2203.     return NULL;
  2204. }
  2205.  
  2206. static object *
  2207. neg(v)
  2208.     object *v;
  2209. {
  2210.     if (v->ob_type->tp_as_number != NULL)
  2211.         return (*v->ob_type->tp_as_number->nb_negative)(v);
  2212.     err_setstr(TypeError, "bad operand type(s) for unary -");
  2213.     return NULL;
  2214. }
  2215.  
  2216. static object *
  2217. pos(v)
  2218.     object *v;
  2219. {
  2220.     if (v->ob_type->tp_as_number != NULL)
  2221.         return (*v->ob_type->tp_as_number->nb_positive)(v);
  2222.     err_setstr(TypeError, "bad operand type(s) for unary +");
  2223.     return NULL;
  2224. }
  2225.  
  2226. static object *
  2227. invert(v)
  2228.     object *v;
  2229. {
  2230.     object * (*f) FPROTO((object *));
  2231.     if (v->ob_type->tp_as_number != NULL &&
  2232.         (f = v->ob_type->tp_as_number->nb_invert) != NULL)
  2233.         return (*f)(v);
  2234.     err_setstr(TypeError, "bad operand type(s) for unary ~");
  2235.     return NULL;
  2236. }
  2237.  
  2238. static object *
  2239. not(v)
  2240.     object *v;
  2241. {
  2242.     int outcome = testbool(v);
  2243.     object *w;
  2244.     if (outcome < 0)
  2245.         return NULL;
  2246.     if (outcome == 0)
  2247.         w = True;
  2248.     else
  2249.         w = False;
  2250.     INCREF(w);
  2251.     return w;
  2252. }
  2253.  
  2254.  
  2255. /* External interface to call any callable object.
  2256.    The arg must be a tuple or NULL. */
  2257.  
  2258. object *
  2259. call_object(func, arg)
  2260.     object *func;
  2261.     object *arg;
  2262. {
  2263.     return PyEval_CallObjectWithKeywords(func, arg, (object *)NULL);
  2264. }
  2265.  
  2266. object *
  2267. PyEval_CallObjectWithKeywords(func, arg, kw)
  2268.     object *func;
  2269.     object *arg;
  2270.     object *kw;
  2271. {
  2272.         ternaryfunc call;
  2273.         object *result;
  2274.  
  2275.     if (arg == NULL)
  2276.         arg = newtupleobject(0);
  2277.     else if (!is_tupleobject(arg)) {
  2278.         err_setstr(TypeError, "argument list must be a tuple");
  2279.         return NULL;
  2280.     }
  2281.     else
  2282.         INCREF(arg);
  2283.  
  2284.     if (kw != NULL && !is_dictobject(kw)) {
  2285.         err_setstr(TypeError, "keyword list must be a dictionary");
  2286.         return NULL;
  2287.     }
  2288.  
  2289.         if (call = func->ob_type->tp_call)
  2290.                 result = (*call)(func, arg, kw);
  2291.         else if (is_instancemethodobject(func) || is_funcobject(func))
  2292.         result = call_function(func, arg, kw);
  2293.     else
  2294.         result = call_builtin(func, arg, kw);
  2295.  
  2296.     DECREF(arg);
  2297.     
  2298.         if (result == NULL && !err_occurred())
  2299.         err_setstr(SystemError,
  2300.                "NULL result without error in call_object");
  2301.         
  2302.         return result;
  2303. }
  2304.  
  2305. static object *
  2306. call_builtin(func, arg, kw)
  2307.     object *func;
  2308.     object *arg;
  2309.     object *kw;
  2310. {
  2311.     if (is_methodobject(func)) {
  2312.         method meth = getmethod(func);
  2313.         object *self = getself(func);
  2314.         int flags = getflags(func);
  2315.         if (!(flags & METH_VARARGS)) {
  2316.             int size = gettuplesize(arg);
  2317.             if (size == 1)
  2318.                 arg = GETTUPLEITEM(arg, 0);
  2319.             else if (size == 0)
  2320.                 arg = NULL;
  2321.         }
  2322.         if (flags & METH_KEYWORDS)
  2323.             return (*(PyCFunctionWithKeywords)meth)(self, arg, kw);
  2324.         if (kw != NULL && getmappingsize(kw) != 0) {
  2325.             err_setstr(TypeError,
  2326.                    "this function takes no keyword arguments");
  2327.             return NULL;
  2328.         }
  2329.         return (*meth)(self, arg);
  2330.     }
  2331.     if (is_classobject(func)) {
  2332.         return newinstanceobject(func, arg, kw);
  2333.     }
  2334.     if (is_instanceobject(func)) {
  2335.             object *res, *call = getattr(func,"__call__");
  2336.         if (call == NULL) {
  2337.             err_clear();
  2338.             err_setstr(AttributeError,
  2339.                    "no __call__ method defined");
  2340.             return NULL;
  2341.         }
  2342.         res = PyEval_CallObjectWithKeywords(call, arg, kw);
  2343.         DECREF(call);
  2344.         return res;
  2345.     }
  2346.     err_setstr(TypeError, "call of non-function");
  2347.     return NULL;
  2348. }
  2349.  
  2350. static object *
  2351. call_function(func, arg, kw)
  2352.     object *func;
  2353.     object *arg;
  2354.     object *kw;
  2355. {
  2356.     object *class = NULL; /* == owner */
  2357.     object *argdefs;
  2358.     object **d, **k;
  2359.     int nk, nd;
  2360.     object *result;
  2361.     
  2362.     if (kw != NULL && !is_dictobject(kw)) {
  2363.         err_badcall();
  2364.         return NULL;
  2365.     }
  2366.     
  2367.     if (is_instancemethodobject(func)) {
  2368.         object *self = instancemethodgetself(func);
  2369.         class = instancemethodgetclass(func);
  2370.         func = instancemethodgetfunc(func);
  2371.         if (self == NULL) {
  2372.             /* Unbound methods must be called with an instance of
  2373.                the class (or a derived class) as first argument */
  2374.             if (gettuplesize(arg) >= 1) {
  2375.                 self = GETTUPLEITEM(arg, 0);
  2376.                 if (self != NULL &&
  2377.                     is_instanceobject(self) &&
  2378.                     issubclass((object *)
  2379.                       (((instanceobject *)self)->in_class),
  2380.                            class))
  2381.                     /* Handy-dandy */ ;
  2382.                 else
  2383.                     self = NULL;
  2384.             }
  2385.             if (self == NULL) {
  2386.                 err_setstr(TypeError,
  2387.        "unbound method must be called with class instance 1st argument");
  2388.                 return NULL;
  2389.             }
  2390.             INCREF(arg);
  2391.         }
  2392.         else {
  2393.             int argcount = gettuplesize(arg);
  2394.             object *newarg = newtupleobject(argcount + 1);
  2395.             int i;
  2396.             if (newarg == NULL)
  2397.                 return NULL;
  2398.             INCREF(self);
  2399.             SETTUPLEITEM(newarg, 0, self);
  2400.             for (i = 0; i < argcount; i++) {
  2401.                 object *v = GETTUPLEITEM(arg, i);
  2402.                 XINCREF(v);
  2403.                 SETTUPLEITEM(newarg, i+1, v);
  2404.             }
  2405.             arg = newarg;
  2406.         }
  2407.     }
  2408.     else {
  2409.         if (!is_funcobject(func)) {
  2410.             err_setstr(TypeError, "call of non-function");
  2411.             return NULL;
  2412.         }
  2413.         INCREF(arg);
  2414.     }
  2415.     
  2416.     argdefs = PyFunction_GetDefaults(func);
  2417.     if (argdefs != NULL && is_tupleobject(argdefs)) {
  2418.         d = &GETTUPLEITEM((tupleobject *)argdefs, 0);
  2419.         nd = gettuplesize(argdefs);
  2420.     }
  2421.     else {
  2422.         d = NULL;
  2423.         nd = 0;
  2424.     }
  2425.     
  2426.     if (kw != NULL) {
  2427.         int pos, i;
  2428.         nk = getmappingsize(kw);
  2429.         k = NEW(object *, 2*nk);
  2430.         if (k == NULL) {
  2431.             err_nomem();
  2432.             DECREF(arg);
  2433.             return NULL;
  2434.         }
  2435.         pos = i = 0;
  2436.         while (mappinggetnext(kw, &pos, &k[i], &k[i+1]))
  2437.             i += 2;
  2438.         nk = i/2;
  2439.         /* XXX This is broken if the caller deletes dict items! */
  2440.     }
  2441.     else {
  2442.         k = NULL;
  2443.         nk = 0;
  2444.     }
  2445.     
  2446.     result = eval_code2(
  2447.         (codeobject *)getfunccode(func),
  2448.         getfuncglobals(func), (object *)NULL,
  2449.         &GETTUPLEITEM(arg, 0), gettuplesize(arg),
  2450.         k, nk,
  2451.         d, nd,
  2452.         class);
  2453.     
  2454.     DECREF(arg);
  2455.     XDEL(k);
  2456.     
  2457.     return result;
  2458. }
  2459.  
  2460. static object *
  2461. apply_subscript(v, w)
  2462.     object *v, *w;
  2463. {
  2464.     typeobject *tp = v->ob_type;
  2465.     if (tp->tp_as_sequence == NULL && tp->tp_as_mapping == NULL) {
  2466.         err_setstr(TypeError, "unsubscriptable object");
  2467.         return NULL;
  2468.     }
  2469.     if (tp->tp_as_mapping != NULL) {
  2470.         return (*tp->tp_as_mapping->mp_subscript)(v, w);
  2471.     }
  2472.     else {
  2473.         int i;
  2474.         if (!is_intobject(w)) {
  2475.             err_setstr(TypeError, "sequence subscript not int");
  2476.             return NULL;
  2477.         }
  2478.         i = getintvalue(w);
  2479.         if (i < 0) {
  2480.             int len = (*tp->tp_as_sequence->sq_length)(v);
  2481.             if (len < 0)
  2482.                 return NULL;
  2483.             i += len;
  2484.         }
  2485.         return (*tp->tp_as_sequence->sq_item)(v, i);
  2486.     }
  2487. }
  2488.  
  2489. static object *
  2490. loop_subscript(v, w)
  2491.     object *v, *w;
  2492. {
  2493.     sequence_methods *sq = v->ob_type->tp_as_sequence;
  2494.     int i;
  2495.     if (sq == NULL) {
  2496.         err_setstr(TypeError, "loop over non-sequence");
  2497.         return NULL;
  2498.     }
  2499.     i = getintvalue(w);
  2500.     v = (*sq->sq_item)(v, i);
  2501.     if (v)
  2502.         return v;
  2503.     if (err_occurred() == IndexError)
  2504.         err_clear();
  2505.     return NULL;
  2506. }
  2507.  
  2508. static int
  2509. slice_index(v, isize, pi)
  2510.     object *v;
  2511.     int isize;
  2512.     int *pi;
  2513. {
  2514.     if (v != NULL) {
  2515.         if (!is_intobject(v)) {
  2516.             err_setstr(TypeError, "slice index must be int");
  2517.             return -1;
  2518.         }
  2519.         *pi = getintvalue(v);
  2520.         if (*pi < 0)
  2521.             *pi += isize;
  2522.     }
  2523.     return 0;
  2524. }
  2525.  
  2526. static object *
  2527. apply_slice(u, v, w) /* return u[v:w] */
  2528.     object *u, *v, *w;
  2529. {
  2530.     typeobject *tp = u->ob_type;
  2531.     int ilow, ihigh, isize;
  2532.     if (tp->tp_as_sequence == NULL) {
  2533.         err_setstr(TypeError, "only sequences can be sliced");
  2534.         return NULL;
  2535.     }
  2536.     ilow = 0;
  2537.     isize = ihigh = (*tp->tp_as_sequence->sq_length)(u);
  2538.     if (isize < 0)
  2539.         return NULL;
  2540.     if (slice_index(v, isize, &ilow) != 0)
  2541.         return NULL;
  2542.     if (slice_index(w, isize, &ihigh) != 0)
  2543.         return NULL;
  2544.     return (*tp->tp_as_sequence->sq_slice)(u, ilow, ihigh);
  2545. }
  2546.  
  2547. static int
  2548. assign_subscript(w, key, v) /* w[key] = v */
  2549.     object *w;
  2550.     object *key;
  2551.     object *v;
  2552. {
  2553.     typeobject *tp = w->ob_type;
  2554.     sequence_methods *sq;
  2555.     mapping_methods *mp;
  2556.     int (*func1)();
  2557.     int (*func2)();
  2558.     if ((mp = tp->tp_as_mapping) != NULL &&
  2559.             (func1 = mp->mp_ass_subscript) != NULL) {
  2560.         return (*func1)(w, key, v);
  2561.     }
  2562.     else if ((sq = tp->tp_as_sequence) != NULL &&
  2563.             (func2 = sq->sq_ass_item) != NULL) {
  2564.         if (!is_intobject(key)) {
  2565.             err_setstr(TypeError,
  2566.             "sequence subscript must be integer (assign or del)");
  2567.             return -1;
  2568.         }
  2569.         else {
  2570.             int i = getintvalue(key);
  2571.             if (i < 0) {
  2572.                 int len = (*sq->sq_length)(w);
  2573.                 if (len < 0)
  2574.                     return -1;
  2575.                 i += len;
  2576.             }
  2577.             return (*func2)(w, i, v);
  2578.         }
  2579.     }
  2580.     else {
  2581.         err_setstr(TypeError,
  2582.                 "can't assign to this subscripted object");
  2583.         return -1;
  2584.     }
  2585. }
  2586.  
  2587. static int
  2588. assign_slice(u, v, w, x) /* u[v:w] = x */
  2589.     object *u, *v, *w, *x;
  2590. {
  2591.     sequence_methods *sq = u->ob_type->tp_as_sequence;
  2592.     int ilow, ihigh, isize;
  2593.     if (sq == NULL) {
  2594.         err_setstr(TypeError, "assign to slice of non-sequence");
  2595.         return -1;
  2596.     }
  2597.     if (sq == NULL || sq->sq_ass_slice == NULL) {
  2598.         err_setstr(TypeError, "unassignable slice");
  2599.         return -1;
  2600.     }
  2601.     ilow = 0;
  2602.     isize = ihigh = (*sq->sq_length)(u);
  2603.     if (isize < 0)
  2604.         return -1;
  2605.     if (slice_index(v, isize, &ilow) != 0)
  2606.         return -1;
  2607.     if (slice_index(w, isize, &ihigh) != 0)
  2608.         return -1;
  2609.     return (*sq->sq_ass_slice)(u, ilow, ihigh, x);
  2610. }
  2611.  
  2612. static int
  2613. cmp_exception(err, v)
  2614.     object *err, *v;
  2615. {
  2616.     if (is_tupleobject(v)) {
  2617.         int i, n;
  2618.         n = gettuplesize(v);
  2619.         for (i = 0; i < n; i++) {
  2620.             /* Test recursively */
  2621.             if (cmp_exception(err, GETTUPLEITEM(v, i)))
  2622.                 return 1;
  2623.         }
  2624.         return 0;
  2625.     }
  2626.     if (is_classobject(v) && is_classobject(err))
  2627.         return issubclass(err, v);
  2628.     return err == v;
  2629. }
  2630.  
  2631. static int
  2632. cmp_member(v, w)
  2633.     object *v, *w;
  2634. {
  2635.     int i, cmp;
  2636.     object *x;
  2637.     sequence_methods *sq;
  2638.     /* Special case for char in string */
  2639.     if (is_stringobject(w)) {
  2640.         register char *s, *end;
  2641.         register char c;
  2642.         if (!is_stringobject(v) || getstringsize(v) != 1) {
  2643.             err_setstr(TypeError,
  2644.                 "string member test needs char left operand");
  2645.             return -1;
  2646.         }
  2647.         c = getstringvalue(v)[0];
  2648.         s = getstringvalue(w);
  2649.         end = s + getstringsize(w);
  2650.         while (s < end) {
  2651.             if (c == *s++)
  2652.                 return 1;
  2653.         }
  2654.         return 0;
  2655.     }
  2656.     sq = w->ob_type->tp_as_sequence;
  2657.     if (sq == NULL) {
  2658.         err_setstr(TypeError,
  2659.             "'in' or 'not in' needs sequence right argument");
  2660.         return -1;
  2661.     }
  2662.     for (i = 0; ; i++) {
  2663.         x = (*sq->sq_item)(w, i);
  2664.         if (x == NULL) {
  2665.             if (err_occurred() == IndexError) {
  2666.                 err_clear();
  2667.                 break;
  2668.             }
  2669.             return -1;
  2670.         }
  2671.         cmp = cmpobject(v, x);
  2672.         XDECREF(x);
  2673.         if (cmp == 0)
  2674.             return 1;
  2675.     }
  2676.     return 0;
  2677. }
  2678.  
  2679. static object *
  2680. cmp_outcome(op, v, w)
  2681.     int op;
  2682.     register object *v;
  2683.     register object *w;
  2684. {
  2685.     register int cmp;
  2686.     register int res = 0;
  2687.     switch (op) {
  2688.     case IS:
  2689.     case IS_NOT:
  2690.         res = (v == w);
  2691.         if (op == (int) IS_NOT)
  2692.             res = !res;
  2693.         break;
  2694.     case IN:
  2695.     case NOT_IN:
  2696.         res = cmp_member(v, w);
  2697.         if (res < 0)
  2698.             return NULL;
  2699.         if (op == (int) NOT_IN)
  2700.             res = !res;
  2701.         break;
  2702.     case EXC_MATCH:
  2703.         res = cmp_exception(v, w);
  2704.         break;
  2705.     default:
  2706.         cmp = cmpobject(v, w);
  2707.         switch (op) {
  2708.         case LT: res = cmp <  0; break;
  2709.         case LE: res = cmp <= 0; break;
  2710.         case EQ: res = cmp == 0; break;
  2711.         case NE: res = cmp != 0; break;
  2712.         case GT: res = cmp >  0; break;
  2713.         case GE: res = cmp >= 0; break;
  2714.         /* XXX no default? (res is initialized to 0 though) */
  2715.         }
  2716.     }
  2717.     v = res ? True : False;
  2718.     INCREF(v);
  2719.     return v;
  2720. }
  2721.  
  2722. static int
  2723. import_from(locals, v, name)
  2724.     object *locals;
  2725.     object *v;
  2726.     object *name;
  2727. {
  2728.     object *w, *x;
  2729.     if (!is_moduleobject(v)) {
  2730.         err_setstr(TypeError, "import-from requires module object");
  2731.         return -1;
  2732.     }
  2733.     w = getmoduledict(v);
  2734.     if (getstringvalue(name)[0] == '*') {
  2735.         int pos, err;
  2736.         object *name, *value;
  2737.         pos = 0;
  2738.         while (mappinggetnext(w, &pos, &name, &value)) {
  2739.             if (!is_stringobject(name) ||
  2740.                 getstringvalue(name)[0] == '_')
  2741.                 continue;
  2742.             if (is_accessobject(value)) {
  2743.                 value = getaccessvalue(value, (object *)NULL);
  2744.                 if (value == NULL) {
  2745.                     err_clear();
  2746.                     continue;
  2747.                 }
  2748.             }
  2749.             else
  2750.                 INCREF(value);
  2751.             err = dict2insert(locals, name, value);
  2752.             DECREF(value);
  2753.             if (err != 0)
  2754.                 return -1;
  2755.         }
  2756.         return 0;
  2757.     }
  2758.     else {
  2759.         x = dict2lookup(w, name);
  2760.         if (x == NULL) {
  2761.             char buf[250];
  2762.             sprintf(buf, "cannot import name %.230s",
  2763.                 getstringvalue(name));
  2764.             err_setstr(ImportError, buf);
  2765.             return -1;
  2766.         }
  2767.         else
  2768.             return dict2insert(locals, name, x);
  2769.     }
  2770. }
  2771.  
  2772. static object *
  2773. build_class(methods, bases, name)
  2774.     object *methods; /* dictionary */
  2775.     object *bases;  /* tuple containing classes */
  2776.     object *name;   /* string */
  2777. {
  2778.     int i;
  2779.     if (!is_tupleobject(bases)) {
  2780.         err_setstr(SystemError, "build_class with non-tuple bases");
  2781.         return NULL;
  2782.     }
  2783.     if (gettuplesize(bases) > 0) {
  2784.         object *base;
  2785.         base = GETTUPLEITEM(bases, 0);
  2786.         /* Call the base's *type*, if it is callable.
  2787.            This code is a hook for Donald Beaudry's type extensions.
  2788.            In unexended Python it will never be triggered since its
  2789.            types are not callable. */
  2790.         if (base->ob_type->ob_type->tp_call) {
  2791.             object *args;
  2792.             object *class;
  2793.             args = mkvalue("(OOO)", name, bases, methods);
  2794.             class = call_object((object *)base->ob_type, args);
  2795.             DECREF(args);
  2796.             return class;
  2797.         }
  2798.     }
  2799.     if (!is_dictobject(methods)) {
  2800.         err_setstr(SystemError, "build_class with non-dictionary");
  2801.         return NULL;
  2802.     }
  2803.     if (!is_stringobject(name)) {
  2804.         err_setstr(SystemError, "build_class witn non-string name");
  2805.         return NULL;
  2806.     }
  2807.     for (i = gettuplesize(bases); --i >= 0; ) {
  2808.         object *base = GETTUPLEITEM(bases, i);
  2809.         if (!is_classobject(base)) {
  2810.             err_setstr(TypeError,
  2811.                 "base is not a class object");
  2812.             return NULL;
  2813.         }
  2814.     }
  2815.     return newclassobject(bases, methods, name);
  2816. }
  2817.  
  2818. static int
  2819. access_statement(name, vmode, f)
  2820.     object *name;
  2821.     object *vmode;
  2822.     frameobject *f;
  2823. {
  2824.     int mode = getintvalue(vmode);
  2825.     object *value, *ac;
  2826.     typeobject *type;
  2827.     int ret;
  2828.     fast_2_locals(f);
  2829.     value = dict2lookup(f->f_locals, name);
  2830.     if (value && is_accessobject(value)) {
  2831.         err_setstr(AccessError, "can't override access");
  2832.         return -1;
  2833.     }
  2834.     err_clear();
  2835.     if (value != NULL && value != None)
  2836.         type = value->ob_type;
  2837.     else
  2838.         type = NULL;
  2839.     ac = newaccessobject(value, f->f_locals, type, mode);
  2840.     if (ac == NULL)
  2841.         return -1;
  2842.     ret = mappinginsert(f->f_locals, name, ac);
  2843.     DECREF(ac);
  2844.     locals_2_fast(f, 0);
  2845.     return ret;
  2846. }
  2847.  
  2848. static int
  2849. exec_statement(prog, globals, locals)
  2850.     object *prog;
  2851.     object *globals;
  2852.     object *locals;
  2853. {
  2854.     char *s;
  2855.     int n;
  2856.     object *v;
  2857.     int plain = 0;
  2858.  
  2859.     if (is_tupleobject(prog) && globals == None && locals == None &&
  2860.         ((n = gettuplesize(prog)) == 2 || n == 3)) {
  2861.         /* Backward compatibility hack */
  2862.         globals = gettupleitem(prog, 1);
  2863.         if (n == 3)
  2864.             locals = gettupleitem(prog, 2);
  2865.         prog = gettupleitem(prog, 0);
  2866.     }
  2867.     if (globals == None) {
  2868.         globals = getglobals();
  2869.         if (locals == None) {
  2870.             locals = getlocals();
  2871.             plain = 1;
  2872.         }
  2873.     }
  2874.     else if (locals == None)
  2875.         locals = globals;
  2876.     if (!is_stringobject(prog) &&
  2877.         !is_codeobject(prog) &&
  2878.         !is_fileobject(prog)) {
  2879.         err_setstr(TypeError,
  2880.                "exec 1st arg must be string, code or file object");
  2881.         return -1;
  2882.     }
  2883.     if (!is_dictobject(globals) || !is_dictobject(locals)) {
  2884.         err_setstr(TypeError,
  2885.             "exec 2nd/3rd args must be dict or None");
  2886.         return -1;
  2887.     }
  2888.     if (dictlookup(globals, "__builtins__") == NULL)
  2889.         dictinsert(globals, "__builtins__", current_frame->f_builtins);
  2890.     if (is_codeobject(prog)) {
  2891.         if (eval_code((codeobject *) prog, globals, locals) == NULL)
  2892.             return -1;
  2893.         return 0;
  2894.     }
  2895.     if (is_fileobject(prog)) {
  2896.         FILE *fp = getfilefile(prog);
  2897.         char *name = getstringvalue(getfilename(prog));
  2898.         if (run_file(fp, name, file_input, globals, locals) == NULL)
  2899.             return -1;
  2900.         return 0;
  2901.     }
  2902.     s = getstringvalue(prog);
  2903.     if (strlen(s) != getstringsize(prog)) {
  2904.         err_setstr(ValueError, "embedded '\\0' in exec string");
  2905.         return -1;
  2906.     }
  2907.     v = run_string(s, file_input, globals, locals);
  2908.     if (v == NULL)
  2909.         return -1;
  2910.     DECREF(v);
  2911.     if (plain)
  2912.         locals_2_fast(current_frame, 0);
  2913.     return 0;
  2914. }
  2915.  
  2916. /* Hack for newimp.py */
  2917. static object *
  2918. find_from_args(f, nexti)
  2919.     frameobject *f;
  2920.     int nexti;
  2921. {
  2922.     int opcode;
  2923.     int oparg;
  2924.     object *list, *name;
  2925.     unsigned char *next_instr;
  2926.     
  2927.     next_instr = GETUSTRINGVALUE(f->f_code->co_code) + nexti;
  2928.     opcode = (*next_instr++);
  2929.     if (opcode != IMPORT_FROM) {
  2930.         INCREF(None);
  2931.         return None;
  2932.     }
  2933.     
  2934.     list = newlistobject(0);
  2935.     if (list == NULL)
  2936.         return NULL;
  2937.     
  2938.     do {
  2939.         oparg = (next_instr[1]<<8) + next_instr[0];
  2940.         next_instr += 2;
  2941.         name = Getnamev(f, oparg);
  2942.         if (addlistitem(list, name) < 0) {
  2943.             DECREF(list);
  2944.             break;
  2945.         }
  2946.         opcode = (*next_instr++);
  2947.     } while (opcode == IMPORT_FROM);
  2948.     
  2949.     return list;
  2950. }
  2951.